package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.dto.AttrGroupEntityDto;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.impl.AttrServiceImpl;
import com.atguigu.gulimall.product.vo.AttrGroupEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author chenhaifu
 * @email 542704865@qq.com
 * @date 2021-03-04 21:22:43
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrServiceImpl attrService;

    /**
     * 获取分类属性分组
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R withattrList(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupEntityDto> data = attrGroupService.withattrList(catelogId);

        return R.ok().put("data", data);
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R listAttr(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrGroupService.selectAttrs(attrgroupId);

        return R.ok().put("data", attrEntities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 批量添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveAttr(@RequestBody List<AttrGroupEntityVo> attrGroupEntityVos) {
        attrGroupService.saveAttrs(attrGroupEntityVos);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 批量删除属性与分组的关联关系
     */
    @DeleteMapping("/attr/relation/delete")
    public R deleteAttrs(@RequestBody List<AttrGroupEntityVo> attrGroupEntityVos) {
        attrGroupService.removeAttrsByIds(attrGroupEntityVos);

        return R.ok();
    }

    /**
     * 获取当前分类下当前属性分组没有关联的其他属性
     *
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> param, @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrService.queryNoAttrs(attrgroupId, param);
        return R.ok().put("page", page);
    }
}
